<script th:fragment="businessJs">

    // 3个参数的名字可以随便命名,但必须是3个参数,少一个都不行
    function retrieveData(sSource, aoData, fnCallback) {
        //赋值参数
        aoData.searchKey = $('#searchKeyInput').val();
        $.ajax({
            url: sSource,//这个就是请求地址对应sAjaxSource
            data: aoData,//这个是把datatable的一些基本数据传给后台,比如起始位置,每页显示的行数
            type: 'post',
            dataType: 'json',
            async: false,
            success: function (result) {
                fnCallback(result);//把返回的数据传给这个方法就可以了,datatable会自动绑定数据的
            },
            error: function (msg) {
            	var r = ajaxSession(msg);
                if(r){
                    msgError(msg.responseText);
            	}
            } 
        });
    }


    // 查询表格   闭包操作后需要 绑定id 追加事件
    var table;
    $("#topSearch").click(function query() {
        table.api().ajax.reload(null, false); // 刷新表格数据，分页信息不会重置
    });

    //表格初始化
    $(document).ready(function () {
        table = $('.dataTables-example').dataTable({
            bFilter: false,    //去掉搜索框
            scrollX:true,
//            bLengthChange: false,   //去掉每页显示多少条数据方法
            "sAjaxSource": "/eng/material/alias/page",//这个是请求的地址
            "fnServerData": retrieveData, // 获取数据的处理函数
            "bServerSide": true, //客户端处理分页
            "dom": '<"top"i>rt<"bottom"flp><"clear">',
            "ordering":false,
            "serverSide" : false,
            "language": {
                "info": ""
            },
            //列index 从0开始算
            "columns": [
				{"data": null,width:100},
                {"data": "id",width:100},
                {"data": "companyName",width:200},
                {"data": "materialId",width:100},
                {"data": "materialCoding",width:100},
                {"data": "materialName",width:100},
                {"data": "materialSpec",width:100},
                {"data": "materialType",width:100},
                {"data": "measurementUName",width:100},
                {"data": "brandName",width:100},
                {"data": "supplierCustomerName",width:230},
                {"data": "supplierCustomerTypeName",width:120},
                {"data": "scMaterialCoding",width:120},
                {"data": "scMaterialName",width:120},
                {"data": "scMaterialSpec",width:120},
                {"data": "creatorId",width:100},
                {"data": "ctime",width:100},
                {"data": "remark",width:100}
            ],
            columnDefs:[
               {
                "targets":0,
                "render": function(data,type,row,mete){
                    var html="<a type='button' href='javascript:void(0)' onclick='editOne("+data.id+")'>修改</a>&nbsp;&nbsp;" +
                            "<a type='button' href='javascript:void(0)'  onclick='deleteOne("+data.id+")'>刪除</a>";
                    return html;
                }
	           },
	           /* {
	                "targets":11,
	                "render": function(data,type,full,mete){
	                	var sct = full.supplierCustomerType;
	                	if(sct=true){
	                		sct ='客户';
	                	}
	                	if(sct=false){
	                		sct ='供应商';
	                	}
	                	return sct;
	                }
		        }, */
	           {
	                "targets":16,
	                "render" : function(data, type, full, meta) {
						//时间格式化
						return  moment(data).format("YYYY-MM-DD");
					}
		       }]
  		 });
    });
    //新增按钮点击事件
    function toAddIndex(){
        window.location.href='/eng/material/alias/toAddIndex';
    }
    //编辑按钮事件
    function editOne(obj){
        window.location.href='/eng/material/alias/toEdit/'+obj;
    }
    //删除按钮事件
    function deleteOne(obj){
        if(confirm("确定要删除当前记录吗？")){
            ajaxPostRequest('/eng/material/alias/deleteMaterial','/eng/material/alias/index',{id:obj});
//            $.post('/department/deleteDepartment',
//                    {name:name, remark:remarks, companyId:companyId},
//                    function (data) {
//                        if(data.code==1){
//                            alert("新增部门成功");
//                            window.location.href='/department/index';
//                        }else{
//                            alert(data.message);
//                        }
//                    });
        }
    }

</script>